package com.controller;




import org.jodconverter.DocumentConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.UUID;

//@Controller
//public class PreviewFile {
//
//    @Autowired
//    private DocumentConverter converter;  //用于转换
//
//    @Value("${upload.path}")
//    String baseUploadPath;
//
//    @ResponseBody
//    @RequestMapping("/testPreview")
//    public void toPdfFile(HttpServletResponse response) {
//        File file = new File(baseUploadPath + "news.docx");//需要转换的文件
//        try {
//            String fileName="news";
//            String fileType=".pdf"; //pdf文件后缀
//            String pdfFile=baseUploadPath+fileName+fileType;  //将这三个拼接起来,就是我们最后生成文件保存的完整访问路径了
//
//            //将文件转化成pdf
//            converter.convert(file).to(new File(pdfFile)).execute();
//
//
//            //使用response,将pdf文件以流的方式发送的前端浏览器上
//            FileInputStream f = new FileInputStream(new File(pdfFile));
//            ServletOutputStream o = response.getOutputStream();
//            response.setContentType("application/pdf;charset=UTF-8");
//            //                attachment 下载模式
//            //                inline     内嵌模式
//            response.setHeader("content-disposition", "inline;filename=" + URLEncoder.encode(fileName, "UTF-8"));
//            response.setHeader("Access-Control-Allow-Headers","*");
//            response.setHeader("Access-Control-Allow-Methods","GET,POST,OPTIONS");
//            response.setHeader("Access-Control-Allow-Origin","*");
//            response.setHeader("Access-Control-Expose-Headers","*");
//            int i = FileCopyUtils.copy(f, o);
//            System.out.println("流已关闭,可预览,该文件字节大小："+i);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//
//
//}
